package com.leadcloud.nim.jsbridge;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.leadcloud.nim.R;
import com.netease.nim.uikit.business.preference.UserPreferences;
import com.netease.nim.uikit.common.activity.UI;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.uinfo.UserService;
import com.netease.nimlib.sdk.uinfo.model.NimUserInfo;

import java.util.Map;

/**
 *
 */

public class ViewInfoActivity extends UI {


    private View ll_content;
    private View no_data_tip;

    @Override
    public boolean isTransStatusBar() {
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.visitor_info_activity);
        initView();
        initData();
    }

    public static final void start(Context context, String content, String accId) {
        Intent intent = new Intent();
        intent.setClass(context, ViewInfoActivity.class);
        intent.putExtra("content", content);
        intent.putExtra("accId", accId);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
        context.startActivity(intent);
    }

    TextView tv_visitor_id;
    TextView tv_sub_info;
    TextView tv_first_site;
    TextView tv_from;
    TextView tv_title;
    TextView tv_server_name;
    TextView tv_is_chat;
    TextView tv_msg_count;
    TextView tv_ip;
    TextView tv_brower_name;
    TextView tv_version;
    TextView tv_size;

    private void initView() {
        ll_content = findView(R.id.ll_content);
        no_data_tip = findView(R.id.no_data_tip);


        tv_visitor_id = findView(R.id.tv_visitor_id);
        tv_sub_info = findView(R.id.tv_sub_info);
        tv_first_site = findView(R.id.tv_first_site);
        tv_from = findView(R.id.tv_from);
        tv_title = findView(R.id.tv_title);
        tv_server_name = findView(R.id.tv_server_name);
        tv_is_chat = findView(R.id.tv_is_chat);
        tv_msg_count = findView(R.id.tv_msg_count);
        tv_ip = findView(R.id.tv_ip);
        tv_brower_name = findView(R.id.tv_brower_name);
        tv_version = findView(R.id.tv_version);
        tv_size = findView(R.id.tv_size);

        findView(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void initData() {
        String accId = getIntent().getStringExtra("accId");
        NimUserInfo user = NIMClient.getService(UserService.class).getUserInfo(accId);
        Map<String, Object> extensionMap = user.getExtensionMap();
        String visitorId = (String) extensionMap.get("visitorId");
        String result = UserPreferences.getString(visitorId);
        if (result.isEmpty()) {
            String content = getIntent().getStringExtra("content");
            if (content==null||content.isEmpty()) {
                no_data_tip.setVisibility(View.VISIBLE);
                ll_content.setVisibility(View.GONE);
            } else {
                inflateData(content);
            }
        } else {
            inflateByRequestData(result);
            no_data_tip.setVisibility(View.GONE);
            ll_content.setVisibility(View.VISIBLE);
        }
    }

    private void inflateData(String content) {
        JSONObject jsonObject = JSONObject.parseObject(content);
        JSONObject result = jsonObject.getJSONObject("xUserInfo").getJSONObject("baseinfo");
        String title = result.getString("title");
        tv_title.setText("标题：" + title);
        tv_visitor_id.setText("访客ID：" + result.getString("visitorId"));
        tv_sub_info.setText("备注：" + result.getString("0"));
        tv_first_site.setText("初次访问网址：" + result.getString("url"));
        tv_from.setText("访问来源：" + result.getString("visitSource"));
        //tv_title.setText("标题：" + result.getString("title"));
        tv_server_name.setText("接待客服：" + result.getString("0"));
        tv_is_chat.setText("是否聊过天：" + result.getString("isFirstVisit"));
        tv_msg_count.setText("游客讯息数：" + result.getString("0"));
        tv_ip.setText("IP定位：" + result.getString("0"));
        tv_brower_name.setText("浏览器：" + result.getString("browser"));
        tv_version.setText("浏览器版本：" + result.getString("browserVersion"));
        tv_size.setText("分辨率：" + result.getString("screen"));
    }

    private void inflateByRequestData(String content) {
        JSONObject result = JSONObject.parseObject(content);
        String title = result.getString("title");
        String haveChatStr = result.getString("haveChat");
        String  haveChat="是";
        if(haveChatStr!=null){
            haveChat=haveChatStr.equals("1")?"是":"否";
        }
        tv_title.setText("标题：" + title);
        tv_visitor_id.setText("访客ID：" + result.getString("visitorId"));
        tv_sub_info.setText("备注：" + result.getString("remark"));
        tv_first_site.setText("初次访问网址：" + result.getString("firstVisitorAddress"));
        tv_from.setText("访问来源：" + result.getString("firstVisitorAddress"));
        tv_server_name.setText("接待客服：" + result.getString("receptionServer"));
        tv_is_chat.setText("是否聊过天：" + haveChat);
        tv_msg_count.setText("游客讯息数：" + result.getString("visitorChatCount"));
        tv_ip.setText("IP定位：" + result.getString("ipAddress")+" "+result.getString("ipLocation"));
        tv_brower_name.setText("浏览器：" + result.getString("browser"));
        tv_version.setText("浏览器版本：" + result.getString("browserVersion"));
        tv_size.setText("分辨率：" + result.getString("screenResolution"));
    }
}
